import Permissions from "@/constant/permission";
import { RouteName } from "@/constant/route";
import { ApiRouteRecord } from "@/types/entity/route";
import { arrayToTreeArray } from "@/utils/arrayUtils";
import { cloneDeep, isEmpty, isString } from "lodash-es";
import { RouteRecordRaw } from "vue-router";

interface MemberStore {
  user: any;
  authRoutes: ApiRouteRecord[];
  permissions: string[];
}
const views = import.meta.glob("@/views/**/index.vue");

const useMemberStore = defineStore("member", {
  state: (): MemberStore => {
    return {
      user: {
        userId: 2,
      },
      authRoutes: [],
      permissions: ["*:*:*"],
    };
  },
  getters: {
    getAuthRoutes(state) {
      const routes = cloneDeep(state.authRoutes).map((route: any) => {
        if (route.component && isString(route.component))
          route.component = views[`/src/views${route.component}`];
        return route;
      });
      return arrayToTreeArray<RouteRecordRaw>(routes);
    },
    getMemberPermissions(state) {
      return state.permissions;
    },
  },
  actions: {
    login(loginInfo?: any) {
      const router = useRouter();
      router.replace({ name: RouteName.ROOT });
    },
    logout() {},
    setAuthRoutes(routes: ApiRouteRecord[]) {
      this.authRoutes = routes;
    },
    fetchUserAuthRoutes() {
      if (!this.user.userId) return;
      this.setAuthRoutes([
        {
          id: 1,
          pid: -1,
          name: "system",
          path: "system",
          meta: { label: "系统管理", icon: "ph:gear" },
        },
        {
          id: 2,
          pid: 1,
          name: "system-member",
          path: "member",
          component: "/system/member/index.vue",
          meta: { label: "成员管理" },
        },
        {
          id: 3,
          pid: 1,
          name: "system-role",
          path: "role",
          component: "/system/role/index.vue",
          meta: { label: "角色管理" },
        },
        {
          id: 4,
          pid: 1,
          name: "system-setting",
          path: "setting",
          component: "/system/setting/index.vue",
          meta: { label: "系统设置" },
        },
        {
          id: 5,
          pid: -1,
          name: "notice",
          path: "notice",
          component: "/notice/index.vue",
          meta: {
            label: "公告管理",
            icon: "ph:chat-circle-text",
          },
        },
      ]);
    },
    hasPermission(needPermission?: string): boolean {
      if (!needPermission) return false;
      if (this.permissions.includes(Permissions.ALL)) return true;
      return this.permissions.includes(needPermission);
    },
    hasAllPermissions(needPermissions?: string[]): boolean {
      if (!needPermissions) return false;
      if (this.permissions.includes(Permissions.ALL)) return true;
      return needPermissions.every((permission) => this.permissions.includes(permission));
    },
    hasOneOfPermission(needPermissions?: string[]): boolean {
      if (!needPermissions) return false;
      if (this.permissions.includes(Permissions.ALL)) return true;
      return needPermissions.some((permission) => this.permissions.includes(permission));
    },
  },
  // persist: true,
});

export default useMemberStore;
